package com.qiche.service.impl;

import com.qiche.aspect.Result;
import com.qiche.mapper.*;
import com.qiche.model.*;
import com.qiche.service.BankService;
import com.qiche.service.ServiceItemWorkstageService;
import com.qiche.service.SubscribeProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Linzw on 2018/01/24.
 */
@Service("serviceItemWorkstageService")
public class ServiceItemWorkstageServiceImpl extends BaseService<ServiceItemWorkstage> implements ServiceItemWorkstageService {

	@Autowired
	private SubscribeProductMapper subscribeProductMapper;
	@Autowired
	private SubscribeMapper subscribeMapper;
	@Autowired
	private WorkstageMapper workstageMapper;
	@Autowired
	private ServiceItemWorkstageMapper serviceItemWorkstageMapper;

	@Transactional
	@Override
	public Result generateWorkstageBySubproId(Long subproId) {

		SubscribeProduct subscribeProduct=subscribeProductMapper.selectByPrimaryKey(subproId);
		if(subscribeProduct ==null){
			return Result.createByErrorMessage("不存在的预约项目");
		}
		Example example=new Example(Workstage.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("itemId",subscribeProduct.getProductId());
		example.setOrderByClause("sort asc");
		List<Workstage> workstageList=workstageMapper.selectByExample(example);
		List<ServiceItemWorkstage> serviceItemWorkstageList=new ArrayList<>();
		for (Workstage workstage:workstageList
			 ) {
			ServiceItemWorkstage itemWorkstage=new ServiceItemWorkstage();
			itemWorkstage.setSubProId(subproId);
			itemWorkstage.setWorkstageSort(workstage.getSort().longValue());
			itemWorkstage.setWorkstageName(workstage.getName());
			itemWorkstage.setIsComplete(0);
			/*if(workstageList.indexOf(workstage)==0){
				itemWorkstage.setIsCurrent(0);
			}else{
				itemWorkstage.setIsCurrent(0);
			}*/
			itemWorkstage.setIsCurrent(0);
			serviceItemWorkstageList.add(itemWorkstage);
		}
		int insertCount=0;
		if(serviceItemWorkstageList.size()>0){
			insertCount= serviceItemWorkstageMapper.insertList(serviceItemWorkstageList);
		}else {
			insertCount=1;
		}

		if(insertCount>0){
			return Result.createBySuccessMessage("生成成功");
		}
		return Result.createByErrorMessage("生成失败");
	}

	@Override
	public List<ServiceItemWorkstage> selectBySubproId(Long subproId) {
		Example example=new Example(ServiceItemWorkstage.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("subProId",subproId);
		example.setOrderByClause("workstage_sort asc");
		List<ServiceItemWorkstage>  serviceItemWorkstageList=serviceItemWorkstageMapper.selectByExample(example);
		return serviceItemWorkstageList;

	}

	@Override
	public int countBySubproId(Long subproId) {
		Example example=new Example(ServiceItemWorkstage.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("subProId",subproId);
		example.setOrderByClause("workstage_sort asc");
		int  count=serviceItemWorkstageMapper.selectCountByExample(example);
		return count;

	}
	@Override
	public Result startWorkStage(Long subscribeId) {
		Subscribe subscribe=subscribeMapper.selectByPrimaryKey(subscribeId);
		Example example=new Example(SubscribeProduct.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("subscribeId",subscribe.getSubscribeId());
		List<SubscribeProduct> subscribeProducts=subscribeProductMapper.selectByExample(example);

		List<ServiceItemWorkstage> serviceItemWorkstages=selectBySubproId(subscribeProducts.get(0).getSubProId());
		ServiceItemWorkstage first=serviceItemWorkstages.get(0);
		first.setIsCurrent(1);
		serviceItemWorkstageMapper.updateByPrimaryKey(first);

		return  Result.createBySuccess(selectBySubproId(subscribeProducts.get(0).getSubProId()));
	}

	@Transactional
	@Override
	public Result nextWorkStage(Long serviceItemWorkstageId) {

 		ServiceItemWorkstage itemWorkstage=	serviceItemWorkstageMapper.selectByPrimaryKey(serviceItemWorkstageId);
		if(itemWorkstage.getIsComplete()==1){
			return Result.createByErrorMessage("该工序已完成,下一步设置失败");
		}
		itemWorkstage.setIsComplete(1);
		itemWorkstage.setIsCurrent(0);
		serviceItemWorkstageMapper.updateByPrimaryKey(itemWorkstage);
		Example example=new Example(ServiceItemWorkstage.class);
		Example.Criteria criteria=example.createCriteria();
		criteria.andEqualTo("subProId",itemWorkstage.getSubProId());
		criteria.andGreaterThan("workstageSort",itemWorkstage.getWorkstageSort());
		example.setOrderByClause("workstage_sort asc");
		List<ServiceItemWorkstage>  serviceItemWorkstageList=serviceItemWorkstageMapper.selectByExample(example);
		if(!serviceItemWorkstageList.isEmpty()){
			ServiceItemWorkstage workstage=serviceItemWorkstageList.get(0);
			workstage.setIsCurrent(1);
			serviceItemWorkstageMapper.updateByPrimaryKey(workstage);
		}
		//更新主表进度字段
		SubscribeProduct subscribeProduct=subscribeProductMapper.selectByPrimaryKey(itemWorkstage.getSubProId());
		Subscribe subscribe=subscribeMapper.selectByPrimaryKey(subscribeProduct.getSubscribeId());
		String[] strArray=subscribe.getProgress().split("/");
		if((Integer.parseInt(strArray[0])+1 )==Integer.parseInt(strArray[1])){
			subscribe.setProgress("已完成");
		}else {
			subscribe.setProgress(Integer.parseInt(strArray[0])+1 +"/" +strArray[1]);
		}
		subscribeMapper.updateByPrimaryKeySelective(subscribe);
		return Result.createBySuccessMessage("下一步设置成功");

	}
}
